using System;
using System.IO;
using UnityEngine;
using Object = UnityEngine.Object;

namespace LS.Digitize
{
    [System.Serializable]
    public class AssetInfo: ICopyable<AssetInfo>, ICloneable<AssetInfo>
    {
        public static readonly AssetInfo Empty = new AssetInfo();
        
        [SerializeField]
        private string m_FileName;
        
        [SerializeField]
        private string m_FileDirectory;
        
        [SerializeField]
        private string m_FileExtension;

        [SerializeField]
        private string m_Guid;
        
        public string FileName
        {
            get { return m_FileName; }
        }

        public string FileDirectory
        {
            get { return m_FileDirectory; }
        }

        public string FileExtension
        {
            get { return m_FileExtension; }
        }

        public string FullPath
        {
            get
            {
                return GetFullPath();
            }
        }

        public string Guid
        {
            get { return m_Guid; }
        }
        
        public AssetInfo(): this(string.Empty,string.Empty,string.Empty)
        {
            
        }
        
        public AssetInfo(string fullPath, string guid = null)
        {
            m_FileName = Path.GetFileNameWithoutExtension(fullPath);
            m_FileDirectory = Path.GetDirectoryName(fullPath);
            m_FileExtension = Path.GetExtension(fullPath);
            m_Guid = guid;
        }
        
        public AssetInfo(string fileName, string fileDirectory, string fileExtension, string guid = null)
        {
            m_FileName = fileName;
            m_FileDirectory = fileDirectory;
            m_FileExtension = fileExtension;
            m_Guid = guid;
        }
        
        public AssetInfo Copy(AssetInfo target)
        {
            m_FileName = target.m_FileName;
            m_FileDirectory = target.m_FileDirectory;
            m_FileExtension = target.m_FileExtension;
            return this;
        }

        public AssetInfo Clone()
        {
            return new AssetInfo(m_FileName, m_FileDirectory, m_FileExtension);
        }

        public override int GetHashCode()
        {
            return GetFullPath().GetHashCode();
        }

        public override string ToString()
        {
            return GetFullPath();
        }

        public override bool Equals(object obj)
        {
            return obj.GetHashCode() == this.GetHashCode();
        }

        private string GetFullPath()
        {
            string fileExtension = m_FileExtension;
            string fileName = m_FileName;
            if (!string.IsNullOrEmpty(fileExtension))
            {
                if (!fileExtension.StartsWith("."))
                {
                    fileExtension =  "." + fileExtension;
                }
                fileName = fileName + fileExtension;
            }
            string fullPath = Path.Combine(m_FileDirectory, $"{fileName}").Replace("\\", "/");
            return fullPath;
        }
    }
}
